package com.cyh;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by yanhuche on 2/28/2017.
 */
public class ForEach_Remove {


    @Test
    public void test_WrongUsage() {
        List<String> a = new ArrayList<>();
        a.add("1");
        a.add("2");
        for (String temp : a) {
            if ("1".equals(temp)) {
                a.remove(temp);
            }
        }
        System.out.println(a);
    }

    @Test
    public void test_WrongUsage_2() {
        List<String> a = new ArrayList<>();
        a.add("1");
        a.add("2");
        for (String temp : a) {
            if ("2".equals(temp)) {
                a.remove(temp);
            }
        }
        System.out.println(a);
    }

    @Test
    public void test_RightUsage() {
        List<String> a = new ArrayList<>();
        a.add("1");
        a.add("2");
        Iterator<String> it = a.iterator();
        while (it.hasNext()) {
            String temp = it.next();
            if ("1".equals(temp)) {
                it.remove();
            }
        }
        System.out.println(a);
    }

    @Test
    public void test_RightUsage_2() {
        List<String> a = new ArrayList<>();
        a.add("1");
        a.add("2");
        Iterator<String> it = a.iterator();
        while (it.hasNext()) {
            String temp = it.next();
            if ("2".equals(temp)) {
                it.remove();
            }
        }
        System.out.println(a);
    }



}
